package ca.uwindsor.cs.deepweb.estimation.experiment.newsgroup;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

import ca.uwindsor.cs.deepweb.estimation.DictionariesFilesContentIndexer;
import ca.uwindsor.cs.deepweb.estimation.FilesContentIndexer;

public class LimitedIndexerforNG extends DictionariesFilesContentIndexer {

	protected int limit;

	public LimitedIndexerforNG(String sourcepath, String indexdir, String encoding,
			boolean verbose, boolean create, String[] ext, int size) {
		super(sourcepath, indexdir, encoding, verbose, create, ext);
		// TODO Auto-generated constructor stub
		globalID = 1;
		limit = size;
	}

	protected void generateIndex(File directory) {
		try {
			File[] dataFiles = directory.listFiles();
			// Add documents to the index
			for (int i = 0; i < dataFiles.length; i++) {
				if (globalID - 1 == limit) {
					break;
				}
				File file = dataFiles[i];
				if (file.isFile()) {
					if (isValidExtension(file.getName())) {
						if (isVerbose) {
							System.out.println("Indexing file "
									+ file.getCanonicalPath());
						}
						Document document = new Document();
						InputStreamReader txtReader = new InputStreamReader(
								new FileInputStream(file), encoding);

						// make the ID from the path shorter - get the file name
//						String filepath = file.getCanonicalPath();
//						int offset1 = filepath
//								.lastIndexOf(java.io.File.separator);
//						int offset2 = filepath.lastIndexOf(".");
//						String word = new String(filepath.substring(
//								offset1 + 1, offset2));
						 String word = String.valueOf(globalID);

						Field FieldPath = new Field(
								FilesContentIndexer.FIELD_ID, word,
								Field.Store.YES, Field.Index.UN_TOKENIZED);
						Field FieldTitle = new Field(
								FilesContentIndexer.FIELD_TITLE, word,
								Field.Store.YES, Field.Index.UN_TOKENIZED);
						Field FieldBody = new Field(
								FilesContentIndexer.FIELD_CONTENT, txtReader,Field.TermVector.YES);

						document.add(FieldPath);
						document.add(FieldTitle);
						document.add(FieldBody);
						indexWriter.addDocument(document);
						txtReader.close();

						globalID++;
					}
				} else if (file.isDirectory()) {
					generateIndex(file);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String SourceDir = new String();
		String IndexDir = new String();
		String encoding = new String("UTF-8");
		boolean isVerbose = false;
		boolean isCreate = true;
		int step = 1;

		String ext[] = new String[] { "xml" };

		if (args.length < 2) {
			System.out
					.println("usage: java -jar DictionariesFilesContentIndexer --SourceDir=sourcefiledirectory --IndexDir=indexfiledirectory --extension=txt,html,... [--encoding=encoding](default=UTF-8) [-verbose] [-create] !");
			System.exit(0);
		} else {
			for (int i = 0; i < args.length; i++) {
				if (args[i].startsWith("--SourceDir=")) {
					SourceDir = args[i].substring(12);
				} else if (args[i].startsWith("--IndexDir=")) {
					IndexDir = args[i].substring(11);
				} else if (args[i].startsWith("--encoding=")) {
					encoding = args[i].substring(11);
				} else if (args[i].equals("-create")) {
					isVerbose = true;
				} else if (args[i].equals("-verbose")) {
					isCreate = true;
				} else if (args[i].startsWith("--extension=")) {
					String tempexts = args[i].substring(12);
					ext = tempexts.split(",");
				} else if (args[i].startsWith("--step=")) {
					step = Integer.parseInt(args[i].substring(7));
				} else {
					System.out
							.println("usage: java -jar LimitedIndexerforGOV2.jar SourceDir=sourcefiledirectory IndexDir=indexfiledirectory [encoding=encoding](default=UTF-8) [-verbose] --step=!");
					System.exit(0);
				}
			}
		}

		if (SourceDir.equals("") || IndexDir.equals("")) {
			System.out
					.println("usage: java -jar LimitedIndexerforGOV2.jar SourceDir=sourcefiledirectory IndexDir=indexfiledirectory [encoding=encoding](default=UTF-8) [-verbose] --step=!");
			System.exit(0);
		} else {
			LimitedIndexerforNG indexer;
			// if (isRecursive) {
			// indexer = new SogouXMLFilesIndexer(SourceDir, IndexDir,
			// encoding,
			// isVerbose, isCreate, ext);
			// } else {
			// indexer = new SogouXMLFilesIndexer (SourceDir, IndexDir,
			// encoding,
			// isVerbose, isCreate, ext);
			// }
			indexer = new LimitedIndexerforNG(SourceDir, IndexDir, encoding,
					isVerbose, isCreate, ext, step);
			indexer.generateIndex();
		}

	}

}
